package com.mycompany.goodsys.core;

import com.mycompany.goodsys.core.AddGood.AddGoodCtrl;
import com.mycompany.goodsys.core.ChangeGood.ChangeGoodCtrl;
import com.mycompany.goodsys.core.SearchGoodView.SearchGoodsCtrl;
import com.mycompany.goodsys.core.RemoveGood.RemoveGoodCtrl;

public class AppStateCtrl {
    public enum AppState{
        MainView,
        SearchGoodView,
        AddGoodView,
        RemoveGoodView,
        ChangeGoodView,
    }

    private static AppState cur_state = AppState.MainView;
    private static boolean is_init = false;
    public static void Update()
    {
        ClearScreen();
        switch(cur_state){
            case MainView:
                MainView.Update();
                break;
            case SearchGoodView:
                SearchGoodsCtrl.Update();
                break;
            case AddGoodView:
                AddGoodCtrl.Update();
                break;
            case RemoveGoodView:
                RemoveGoodCtrl.Update();
                break;
            case ChangeGoodView:
                ChangeGoodCtrl.Update();
                break;
        }
    }

    private static void ClearScreen(){
        System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n");
        try
        {
            String os = System.getProperty("os.name");
            if (os.contains("Windows")) {
                Runtime.getRuntime().exec("cls");
            }
            else {
                Runtime.getRuntime().exec("clear");
            }
        }
        catch (Exception exception)
        {
            //  Handle exception.
        }
    }

    private static void In()
    {
        switch(cur_state){
            case MainView:
                MainView.Init();
                break;
            case SearchGoodView:
                SearchGoodsCtrl.Init();
                break;
            case AddGoodView:
                AddGoodCtrl.Init();
                break;
            case RemoveGoodView:
                RemoveGoodCtrl.Init();
                break;
            case ChangeGoodView:
                ChangeGoodCtrl.Init();
                break;
        }
    }

    private static void Out()
    {
        switch(cur_state){
            case MainView:
                MainView.Out();
                break;
            case SearchGoodView:
                SearchGoodsCtrl.Out();
                break;
            case AddGoodView:
                AddGoodCtrl.Out();
                break;
            case RemoveGoodView:
                RemoveGoodCtrl.Out();
                break;
            case ChangeGoodView:
                ChangeGoodCtrl.Out();
                break;
        }
    }

    public static void Init()
    {
        if(!is_init){
            In();
            is_init = true;
        }
    }

    public static void ChangeState(AppState state){
        if(cur_state == state){
            System.out.println("状态是相同的不需要转换" + state.toString());
            return;
        }

        Out();
        cur_state = state;
        In();
    }

}
